home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #14 / Monster Media No. 14 (April 1996) (Monster Media, Inc.).ISO / prog_d / oleauttr.zip / XLOLE7.ZIP / XLDRIV7.PAS < prev    next >
Pascal/Delphi Source File  |  1996-01-04  |  2KB  |  94 lines

  1. unit Xldriv7;
  2.  
  3. interface
  4.  
  5. uses
  6.   Oleauto, XLCls, SafeOle,
  7.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  8.   Forms, Dialogs, StdCtrls, ToCtrl, Menus;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     OleAutoClient1: TOleAutoClient;
  13.     btnSaveSheet: TButton;
  14.     btnQuit: TButton;
  15.     SaveDialog1: TSaveDialog;
  16.     edCellText: TEdit;
  17.     Label2: TLabel;
  18.     btnStartExcel: TButton;
  19.     procedure btnQuitClick(Sender: TObject);
  20.     procedure btnSaveSheetClick(Sender: TObject);
  21.     procedure btnStartExcelClick(Sender: TObject);
  22. end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.   ExcelApp : TExcelApp;
  27.  
  28. implementation
  29.  
  30. {$R *.DFM}
  31.  
  32. procedure TForm1.btnQuitClick(Sender: TObject);
  33. begin
  34.   if Assigned(ExcelApp) then
  35.   begin
  36.     ExcelApp.Quit;
  37.     ExcelApp.Release;
  38.     ExcelApp := nil;
  39.   end;
  40.   TSafeOleObject.FreeOobList;
  41.  
  42.   Close;
  43. end;
  44.  
  45. procedure TForm1.btnSaveSheetClick(Sender: TObject);
  46. var
  47.   i : Integer;
  48.   FirstCell : TExcelCells;
  49.   XLActiveSheet : TExcelSheet;
  50.   XLNewBook : TXLWorkBook;
  51. begin
  52.   if Assigned(ExcelApp) then
  53.   begin
  54.     XLNewBook := ExcelApp.WorkBooks.Add;
  55.     XLActiveSheet := XLNewBook.ActiveSheet;
  56.     FirstCell := XLActiveSheet.Cells[1,1];
  57.     FirstCell.Value := edCellText.Text;
  58.     if SaveDialog1.Execute then
  59.       XLActiveSheet.FileSave(SaveDialog1.FileName)
  60.     else
  61.       { Mark it so Excel doesn't ask us if we want to save it. }
  62.       XLNewBook.Saved := True;
  63.     btnQuitClick(Sender); {Shut down and exit. }
  64.   end;
  65. end;
  66.  
  67. procedure TForm1.btnStartExcelClick(Sender: TObject);
  68. begin
  69.   { Protection block for Ooblist memory allocation. }
  70.   try
  71.   { Create a list to keep track of created OLE objects. }
  72.   TSafeOleObject.CreateOobList;
  73.     { Protection block for OLE Automation. }
  74.     try
  75.       { Tell OLE to start up Excel. }
  76.       ExcelApp := TExcelApp.CreateObject('Excel.Application');
  77.       ExcelApp.Visible := True;
  78.       { Move this app to top of Z-order }
  79.       SetWindowPos(Handle, HWND_TOP, 0,0,0,0, SWP_NOMOVE Or SWP_NOSIZE);
  80.     except
  81.       ShowMessage('Caught an exception.  Excel may not be ' +
  82.           'installed correctly.  Otherwise, did you remember ' +
  83.           'to drop a TOLEAutoClient component on your form?');
  84.       btnQuitClick(Sender);
  85.     end;
  86.   except
  87.     ShowMessage('Not enough memory for internal record keeping.');
  88.   end;
  89.  
  90. end;
  91.  
  92. end.
  93.  
  94.